package com.control.qct.businesssteward.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.model.OrderListModel;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.control.qct.businesssteward.utils.GlideUtil;

import java.util.List;

/**
 * Created by Administrator on 2018/1/9 0009.
 */

public class OrderAdapter extends BaseQuickAdapter<OrderListModel.DatasBean, BaseViewHolder> {
    public OrderAdapter(@Nullable List<OrderListModel.DatasBean> data) {
        super(R.layout.rv_order_item, data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, OrderListModel.DatasBean item) {


        holder.setText(R.id.tv_order_code, "订单号：" + item.getOrderSn()).setText(R.id.tv_order_goods_num, "共" + item
                .getProductCount() + "件商品").setText(R.id.tv_order_status, item.getState()).setText(R.id
                .tv_order_goods_price, "￥" + DecimalFormatUtils.DecimalFormatTwo(item.getAmount())).setText(R.id
                .tv_goods_name, item.getDetails().size() > 0 ? item.getDetails().get(0).getTitle() : "").setGone(R.id
                .rl_gathering, item.getState().equals("待收款")).setGone(R.id.ll_single_goods, item.getDetails().size()
                == 1).setGone(R.id.rl_goods_information, item.getDetails().size() > 0);
        GlideUtil.loadImage(mContext, item.getDetails().size() > 0 ? item.getDetails().get(0).getImageUrl() : "",
                (ImageView) holder.getView(R.id.iv_goods_picture));
        RecyclerView rvOrderGoods = holder.getView(R.id.rv_order_goods);
        rvOrderGoods.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        OrderGoodsAdapter orderGoodsAdapter = new OrderGoodsAdapter(item.getDetails());
        rvOrderGoods.setAdapter(orderGoodsAdapter);
        orderGoodsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mICallbackClickListener != null) {
                    mICallbackClickListener.onClickListener(holder.getAdapterPosition());
                }
            }
        });
        holder.addOnClickListener(R.id.tv_gathering);


    }

    class OrderGoodsAdapter extends BaseQuickAdapter<OrderListModel.DatasBean.DetailsBean, BaseViewHolder> {

        public OrderGoodsAdapter(@Nullable List<OrderListModel.DatasBean.DetailsBean> data) {
            super(R.layout.rv_order_goods_item, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, OrderListModel.DatasBean.DetailsBean item) {
            GlideUtil.loadImage(mContext, item.getImageUrl(), (ImageView) holder.getView(R.id.iv_goods_picture));
        }
    }

    private ICallbackClickListener mICallbackClickListener;

    public void setCallbackClickListener(ICallbackClickListener listener) {
        this.mICallbackClickListener = listener;
    }

    public interface ICallbackClickListener {
        void onClickListener(int position);
    }
}
